package versionControl.aspectj.test;

import versionControl.aspectj.library.*;

aspect Ordering {
    declare precedence: UpdateObjectAspect, Update;
}

public aspect UpdateObjectAspect {
	// When setting field to 0, generate an update action 
	// causing the set to be performed on the second previous version
	
	pointcut versionedUpdate(ObjectVersionControlChecker obj, Integer newVal):
	set(Integer ObjectVersionControlChecker.field1) && target(obj) && args(newVal);
		
	before(ObjectVersionControlChecker obj, Integer newVal): versionedUpdate(obj, newVal)
	{		
		Integer verID = obj.getVersionID() - 2;
		if (newVal == 0 && obj.getVersionByID(verID) != null)
		{
			obj.setVersionID(verID);
		}
	}
	after(ObjectVersionControlChecker obj, Integer newVal): versionedUpdate(obj, newVal)
	{
		if (newVal == 0 && obj.getLatestVersionID() != -1)
			obj.setVersionID(obj.getLatestVersionID());
	}
}